package com.duolebo.uteped_sdk.remind;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.provider.MediaStore;

import androidx.annotation.NonNull;

import com.duolebo.uteped_sdk.utils.PermissionHelper;
import com.duolebo.uteped_sdk.utils.Tools;
import com.yc.nadalsdk.bean.CameraControl;
import com.yc.nadalsdk.bean.CameraStatusConfig;
import com.yc.nadalsdk.bean.Notify;
import com.yc.nadalsdk.ble.open.UteBleDevice;
import com.yc.nadalsdk.constants.NotifyType;

public class CameraRemind extends BaseRemind {

    private final Context context;

    private boolean isCameraOpen = false;

    @SuppressLint("StaticFieldLeak")
    private static CameraRemind instance;
    public static CameraRemind getInstance(Context context) {
        if (instance == null) {
            instance = new CameraRemind(context);
        }

        return instance;
    }

    public static CameraRemind shareInstance() {
        return instance;
    }


    public CameraRemind(Context context) {
        super(context);
        this.context = context;
    }

    private void openCameraApp() {
        try {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);

            uteBleConnection.setCameraStatus(CameraStatusConfig.STATUS_OPEN);
            uteBleConnection.setCameraStatus(CameraStatusConfig.STATUS_ICON);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void closeCameraApp() {

    }

    private void takePhoto(){

    }

    @Override
    public void onNotify(@NonNull UteBleDevice uteBleDevice, @NonNull Notify notify) {
        if (notify.getType() == NotifyType.CAMERA_CONTROL) {
            CameraControl cameraControl = (CameraControl) notify.getData();
            switch (cameraControl.getInstruction()) {
                case CameraControl.INSTRUCTION_OPEN_CAMERA:
//                    if (PermissionHelper.hasPermission(Manifest.permission.CAMERA)) {
//                        openCameraApp();
//                    }
//                    else {
//                        PermissionHelper.getInstance().checkPermission(Tools.getActivity(), Manifest.permission.CAMERA, success -> {
//                            if (success) {
//                                openCameraApp();
//                            }
//                        });
//                    }
                    isCameraOpen = true;
                    break;

                case CameraControl.INSTRUCTION_CLOSE_CAMERA:
//                    closeCameraApp();
                    isCameraOpen = false;
                    break;

                case CameraControl.INSTRUCTION_PHOTOGRAPH:
//                    takePhoto();
                    break;
            }
        }
    }

    public boolean isCameraOpen() {
        return isCameraOpen;
    }

    public void resetCameraStatus() {
        isCameraOpen = false;
    }
}
